package com.yunqing.restkeeper.cash.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunqing.restkeeper.cash.web.vo.TablePanelVO;
import com.yunqing.restkeeper.cash.web.vo.TableVO;
import com.yunqing.restkeeper.common.constants.SystemCode;
import com.yunqing.restkeeper.store.api.entity.Table;
import com.yunqing.restkeeper.store.api.entity.TableLog;
import com.yunqing.restkeeper.store.api.service.ITableAreaService;
import com.yunqing.restkeeper.store.api.service.ITableLogService;
import com.yunqing.restkeeper.store.api.service.ITableService;
import com.yunqing.restkeeper.web.common.response.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName TableController
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/8/22
 */
@RestController
@RequestMapping("/table")
@Tag(name = "收银端区域桌台接口")
public class TableController {

    @DubboReference(version = "1.0.0", check = false)
    private ITableAreaService tableAreaService;

    @DubboReference(version = "1.0.0", check = false)
    private ITableLogService tableLogService;

    @DubboReference(version = "1.0.0",check = false)
    private ITableService tableService;

    @Operation(summary = "区域列表接口")
    @GetMapping("/listTableArea")
    public List<Map<String, Object>> list() {
        return tableAreaService.listTableArea();
    }

    @Operation(summary = "开桌")
    @PutMapping("/openTable/{tableId}/{numbers}")
    public boolean openTable(@PathVariable String tableId, @PathVariable Integer numbers) {
        TableLog tableLog = new TableLog();
        tableLog.setTableId(tableId);
        tableLog.setUserNumbers(numbers);
        return tableLogService.openTable(tableLog);
    }

    @Operation(summary = "桌台面板")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "areaId", description = "区域Id", required = true, schema = @Schema(type = "String")),
            @Parameter(in = ParameterIn.PATH, name = "page", description = "页码", required = true,schema = @Schema(type = "Integer")),
            @Parameter(in = ParameterIn.PATH, name = "pageSize", description = "每页数量", required = true, schema = @Schema(type = "Integer"))})
    @GetMapping("/search/{areaId}/{page}/{pageSize}")
    public TablePanelVO queryByArea(@PathVariable String areaId, @PathVariable int page, @PathVariable int pageSize) {

        TablePanelVO tablePanelVO = new TablePanelVO();

        //桌台统计信息
        tablePanelVO.setFreeNumbers(tableService.countTableByStatus(areaId, SystemCode.TABLE_STATUS_FREE));
        tablePanelVO.setLockedNumbers(tableService.countTableByStatus(areaId, SystemCode.TABLE_STATUS_LOCKED));
        tablePanelVO.setOpenedNumbers(tableService.countTableByStatus(areaId, SystemCode.TABLE_STATUS_OPENED));

        //桌台面板详情，支持分页
        IPage<Table> pageInfo = tableService.queryPageByAreaId(areaId, page, pageSize);

        List<TableVO> tableVOList = Lists.newArrayList();

        pageInfo.getRecords().forEach(d -> {


            TableVO tableVO = new TableVO();

            tableVO.setTableId(d.getTableId());
            tableVO.setTableName(d.getTableName());

            //如果是开桌状态
            if (d.getStatus() == SystemCode.TABLE_STATUS_OPENED) {

                //从tableLog中来获取相关数据内容
                TableLog tableLog = tableLogService.getOpenTableLog(d.getTableId());
                tableVO.setCreateTime(tableLog.getCreateTime());
                tableVO.setUserNumbers(tableLog.getUserNumbers());
            }

            tableVOList.add(tableVO);
        });

        PageVO<TableVO> pageVO = new PageVO<>(pageInfo, tableVOList);
        tablePanelVO.setTablePage(pageVO);


        return tablePanelVO;
    }
}
